package glemipsc;

import glemipsc.implementations.Argument;
import glemipsc.implementations.Implementation;
import glemipsc.instructions.Call;
import glemipsc.instructions.Expression;
import glemipsc.instructions.VariableRef;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class StartFunction extends Function {
	private ArrayList initInstructions = new ArrayList();
	private Linker linker;

	public StartFunction() {
		super("_start");
	}
	
	public void setLinker(Linker linker) {
		this.linker = linker;
	}
	
	public void initializeVariables(HashMap<String, SymbolElement> symbols) {
		Implementation impl = GLEMipsC.compiler.environment.implementation;
		
		Iterator<String> siter = symbols.keySet().iterator();
		while (siter.hasNext()) {
			SymbolElement el = symbols.get(siter.next());
			if (!el.getType().isStatic())
				continue;
			if (el.getInitialValue() == null)
				continue;
			Argument res = impl.getFreeRegister();
			el.computeInitialization(res, initInstructions, this);
			impl.freeRegister(res);
		}
		
		// Adding call to "main" function
		initInstructions.add(new Call(linker.getFunction("main"), new ArrayList()));
		
		this.setInstructions(initInstructions);
	}
}
